/*
 * @Author: 郭桓
 * @Date: 2019-02-14 14:15:40
 * @LastEditors: GuoHuan
 * @LastEditTime: 2019-03-08 10:51:19
 * @Description: file content
 */

const Utils = require('Utils');
cc.Class({
    extends: cc.Component,

    properties: {

    },

    start() {
        this.node.active = false;
        //判断是有对应的账号
        this._payInfo = null; //
    },

    isWeiXin() {
        //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
        var ua = window.navigator.userAgent.toLowerCase();
        //通过正则表达式匹配ua中是否含有MicroMessenger字符串
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            return true;
        } else {
            return false;
        }
    },

    onBridgeReady() {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', this._payInfo,
            function(res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    // 使用以上方式判断前端返回,微信团队郑重提示：
                    //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    let uiJS = cc.director.getScene().getChildByName('Canvas').getComponent('MainActivity');
                    if (uiJS == null) {
                        uiJS = cc.director.getScene().getChildByName('Canvas').getComponent('GameActivity');
                    }
                    if (uiJS) {
                        Utils.singleton.startLogin(function(err, data) {
                            if (err == null) {
                                // cc.log("有的。开始刷新");
                                Utils.singleton.userinfo = data.result;
                                Utils.singleton.userinfo.loginId = data.loginId;
                                // data.loginId = loginValue.loginId;
                                Utils.singleton.showShop();
                                uiJS.updateGold();
                            }
                        }.bind(this), '刷新金币,请稍等');
                    }
                }
            });
    },

    onViewClick(sender, data) {
        switch (data) {
            case 'close':
                {
                    Utils.singleton.loadingNode.active = false;
                    this.node.active = false;
                }
                break;
            case 'buy1':
                {
                    let totalFee = 1000;
                    var openid = Utils.singleton.getUrlParam('openId');
                    if (openid == null || openid.length == 0) {
                        Utils.singleton.request('buyGoods/h5Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                        }, function(err, data) {
                            if (err == null) {
                                window.location.href = data.result.xml.mweb_url;
                            }
                        }.bind(this));
                    } else {

                        Utils.singleton.request('buyGoods/Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                            openId: openid
                        }, function(err, data) {
                            if (err == null) {
                                cc.log(" data = " + data);
                                this._payInfo = data.wxInfo;
                                if (typeof WeixinJSBridge == "undefined") {
                                    if (document.addEventListener) {
                                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                    } else if (document.attachEvent) {
                                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                    }
                                } else {
                                    this.onBridgeReady();
                                }
                            }
                        }.bind(this));
                    }

                }
                break;
            case 'buy2':
                {
                    let totalFee = 1800;
                    var openid = Utils.singleton.getUrlParam('openId');
                    if (openid == null || openid.length == 0) {
                        Utils.singleton.request('buyGoods/h5Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                        }, function(err, data) {
                            if (err == null) {
                                window.location.href = data.result.xml.mweb_url;
                            }
                        }.bind(this));
                    } else {

                        Utils.singleton.request('buyGoods/Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                            openId: openid
                        }, function(err, data) {
                            if (err == null) {
                                cc.log(" data = " + data);
                                this._payInfo = data.wxInfo;
                                if (typeof WeixinJSBridge == "undefined") {
                                    if (document.addEventListener) {
                                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                    } else if (document.attachEvent) {
                                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                    }
                                } else {
                                    this.onBridgeReady();
                                }
                            }
                        }.bind(this));

                    }
                }
                break;
            case 'buy3':
                {
                    let totalFee = 2800;
                    var openid = Utils.singleton.getUrlParam('openId');
                    if (openid == null || openid.length == 0) {
                        Utils.singleton.request('buyGoods/h5Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                        }, function(err, data) {
                            if (err == null) {
                                window.location.href = data.result.xml.mweb_url;
                            }
                        }.bind(this));
                    } else {

                        Utils.singleton.request('buyGoods/Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                            openId: openid
                        }, function(err, data) {
                            if (err == null) { 
                                this._payInfo = data.wxInfo;
                                if (typeof WeixinJSBridge == "undefined") {
                                    if (document.addEventListener) {
                                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                    } else if (document.attachEvent) {
                                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                    }
                                } else {
                                    this.onBridgeReady();
                                }
                            }
                        }.bind(this));
                    }
                }
                break;
            case 'buy4':
                {
                    let totalFee = 6800;
                    var openid = Utils.singleton.getUrlParam('openId');
                    if (openid == null || openid.length == 0) {
                        Utils.singleton.request('buyGoods/h5Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                        }, function(err, data) {
                            if (err == null) {
                                window.location.href = data.result.xml.mweb_url;
                            }
                        }.bind(this));
                    } else {

                        Utils.singleton.request('buyGoods/Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                            openId: openid
                        }, function(err, data) {
                            if (err == null) {
                                cc.log(" data = " + data);
                                this._payInfo = data.wxInfo;
                                if (typeof WeixinJSBridge == "undefined") {
                                    if (document.addEventListener) {
                                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                    } else if (document.attachEvent) {
                                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                    }
                                } else {
                                    this.onBridgeReady();
                                }
                            }
                        }.bind(this));
                    }
                }
                break;
            case 'buy5':
                {
                    let totalFee = 9800;
                    var openid = Utils.singleton.getUrlParam('openId');
                    if (openid == null || openid.length == 0) {
                        Utils.singleton.request('buyGoods/h5Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                        }, function(err, data) {
                            if (err == null) {
                                window.location.href = data.result.xml.mweb_url;
                            }
                        }.bind(this));
                    } else {

                        Utils.singleton.request('buyGoods/Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                            openId: openid
                        }, function(err, data) {
                            if (err == null) {
                                cc.log(" data = " + data);
                                this._payInfo = data.wxInfo;
                                if (typeof WeixinJSBridge == "undefined") {
                                    if (document.addEventListener) {
                                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                    } else if (document.attachEvent) {
                                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                    }
                                } else {
                                    this.onBridgeReady();
                                }
                            }
                        }.bind(this));
                    }
                }
                break;
            case 'buy6':
                {
                    let totalFee = 29800;
                    var openid = Utils.singleton.getUrlParam('openId');
                    if (openid == null || openid.length == 0) {
                        Utils.singleton.request('buyGoods/h5Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                        }, function(err, data) {
                            if (err == null) {
                                window.location.href = data.result.xml.mweb_url;
                            }
                        }.bind(this));
                    } else {

                        Utils.singleton.request('buyGoods/Order', 'GET', {
                            totalFee: totalFee,
                            userId: Utils.singleton.userinfo.id,
                            account: Utils.singleton.userinfo.account,
                            loginVar: Utils.singleton.userinfo.loginId,
                            openId: openid
                        }, function(err, data) {
                            if (err == null) {
                                cc.log(" data = " + data);
                                this._payInfo = data.wxInfo;
                                if (typeof WeixinJSBridge == "undefined") {
                                    if (document.addEventListener) {
                                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                    } else if (document.attachEvent) {
                                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                    }
                                } else {
                                    this.onBridgeReady();
                                }
                            }
                        }.bind(this));
                    }
                }
                break;
            case 'music':
                {
                    if(Utils.singleton.loadingNode!=null){
                        Utils.singleton.loadingNode.getComponent('AppStart').onSoundClick();
                    }

                    
                }
                break;
        }
    }

    // update (dt) {},
});